#!/bin/sh

set -e

. $CETIC_6LBR_CONF
. $1/6lbr-functions

config_default

MODE_6LBR=$2
DEV=$3
OS=`uname`

if [ "$BRIDGE" != "1" ]; then
	exit
fi

if [ $MODE_6LBR != "tap" ]; then
	exit
fi

case $OS in
	Linux)
		if [ \( "$CREATE_BRIDGE" = "6LBR" -o "$CREATE_BRIDGE" = "1" -o "$CREATE_BRIDGE" = "LAZY" \) -a \( `brctl show | grep $DEV_BRIDGE | wc -l` = "0" \) ]; then
			echo "6lbr-ifup: Create bridge $DEV_BRIDGE"
			brctl addbr $DEV_BRIDGE
			brctl setfd $DEV_BRIDGE 0
			if [ "$ETH_JOIN_BRIDGE" = "1" ]; then
				echo "6lbr-ifup: Attach device $DEV_ETH to bridge $DEV_BRIDGE"
				brctl addif $DEV_BRIDGE $DEV_ETH
				ip addr flush dev $DEV_ETH
			fi
			ip link set $DEV_BRIDGE up
		else
			:
		fi
		echo "6lbr-ifup: Attach device $DEV to bridge $DEV_BRIDGE"
		ip addr flush dev $DEV
		brctl addif $DEV_BRIDGE $DEV
		;;

	Darwin)
		#TODO: add condition of brctl output as done on line 24 for Linux
		if [ "$CREATE_BRIDGE" = "6LBR" -o "$CREATE_BRIDGE" = "1" -o "$CREATE_BRIDGE" = "LAZY" ]; then
			echo "6lbr-ifup: Create bridge $DEV_BRIDGE and attach device $DEV_ETH"
			ifconfig $DEV_BRIDGE create
			ifconfig $DEV_BRIDGE up addm $DEV_ETH
		else
			echo "6lbr-ifup: Attach device $DEV_ETH to bridge $DEV_BRIDGE"
		fi
		ifconfig $DEV_BRIDGE addm $DEV
		;;

	*)
		echo Unknown OS
		;;
esac
